﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace H.Y.Clipboard
{
	public class Message
	{
		private class SignUpRequest
		{
			public string action;
			public string nickname;
			public string account;
			public string password;
		}

		public class SignUpResponse
		{
			public string action;
			public string status;
			public string uid;
			public string reason;
		}

		private class LoginRequest
		{
			public string action;
			public string account;
			public string password;
		}

		public class LoginResponse
		{
			public string action;
			public string status;
			public string uid;
			public string nickname;
			public string account;
			public string reason;
		}

		private class PushRequest
		{
			public string action;
			public string uid;
			public string time;
			public string device;
			public string content;
		}

		public class PushResponse
		{
			public string action;
			public string status;
			public string uid;
			public string cid;
			public string reason;
		}

		private class FetchRequest
		{
			public string action;
			public string cid;
		}

		public class FetchResponse {
			public string action;
			public string status;
			public string reason;
			public string cid;
			public string content;
			public string time;
			public bool read;
			public string device;
		}
		
		private class QueryRequest
		{
			public string action;
			public string uid;
			public int start;
			public int count;
		}
		
		public class QueryResponse
		{
			public string action;
			public string status;
			public string reason;
			public int start;
			public List<QueryResponseItem> items;
		}

		public class QueryResponseItem
		{
			public string action;
			public string cid;
			public string content;
		}

		private class AliveRequest
		{
			public string action;
		}

		public class AliveResponse
		{
			public string action;
			public string status;
		}

		private class ReadRequest
		{
			public string action;
			public string cid;
		}

		public class ReadResponse
        {
			public string action;
			public string reason;
			public string status;
        }

		public static string generateReadRequest(string cid)
        {
			ReadRequest request = new ReadRequest();
			request.action = Macros.Actions.READ;
			request.cid = cid;
			return JsonConvert.SerializeObject(request);
        }

		public static string generateSignUpRequest(string nickname, string account, string password)
		{
            byte[] pwd = Encoding.UTF8.GetBytes(account + password);
			MD5 md5 = new MD5CryptoServiceProvider();
            byte[] s_pwd = md5.ComputeHash(pwd);
			SignUpRequest request = new SignUpRequest();
			request.action = Macros.Actions.SIGNUP;
			request.account = account;
			request.password = BitConverter.ToString(s_pwd).Replace("-", "");
			request.nickname = nickname;
			return JsonConvert.SerializeObject(request);
		}

		public static string generateLoginRequest(string account, string password)
		{
			byte[] pwd = Encoding.UTF8.GetBytes(account + password);
			MD5 md5 = new MD5CryptoServiceProvider();
			byte[] s_pwd = md5.ComputeHash(pwd);
			LoginRequest request = new LoginRequest();
			request.action = Macros.Actions.LOGIN;
			request.account = account;
			request.password = BitConverter.ToString(s_pwd).Replace("-","");
			return JsonConvert.SerializeObject(request);
		}

		public static string generatePushRequest(string uid, string time, string content, string deviceName)
		{
			PushRequest request = new PushRequest();
			request.action = Macros.Actions.PUSH;
			request.uid = uid;
			request.time = time;
			request.content = content;
			request.device = deviceName;
			return JsonConvert.SerializeObject(request);
		}

		public static string generateFetchRequest(string cid)
		{
			FetchRequest request = new FetchRequest();
			request.action = Macros.Actions.FETCH;
			request.cid = cid;
			return JsonConvert.SerializeObject(request);
		}

		public static string generateQueryRequest(string uid, int start=0, int count=10)
		{
			QueryRequest request = new QueryRequest();
			request.action = Macros.Actions.QUERY;
			request.uid = uid;
			request.start = start;
			request.count = count;
			return JsonConvert.SerializeObject(request);
		}

		public static string generateAliveRequest()
		{
			AliveRequest request = new AliveRequest();
			request.action = Macros.Actions.ALIVE;
			return JsonConvert.SerializeObject(request);
		}
	}
}
